﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PerfumeShopManagement.Model;
using PerfumeShopManagement.Utility;

namespace PerfumeShopManagement.DAL
{
    public class DalAccount : DalAbstractBase<User, string>
    {
        public override User GetObjectById(string id)
        {
            string username = id;
            var result = (from u in _context.Users
                          where u.Username.Equals(username)
                          select u).FirstOrDefault();
            return result;
        }

        public override List<User> GetAllObjects()
        {
            throw new NotImplementedException();
        }

        public override List<User> GetObjectsByCriteria(User obj)
        {
            User user = obj;

            var usersQuery = (from u in _context.Users
                              select u);

            if (user.RoleId != 0)
                usersQuery = usersQuery.Where(u => u.RoleId.Equals(user.RoleId));

            if (!StringUtil.isEmptyOrBlank(user.Username))
                usersQuery = usersQuery.Where(u => u.Username.Contains(user.Username));
   
            if (!StringUtil.isEmptyOrBlank(user.Password))
                usersQuery = usersQuery.Where(u => u.Password.Contains(user.Password));

            if (!StringUtil.isEmptyOrBlank(user.Name))
                usersQuery = usersQuery.Where(u => u.Name.Contains(user.Name));

            if (!StringUtil.isEmptyOrBlank(user.Address))
                usersQuery = usersQuery.Where(u => u.Address.Contains(user.Address));

            if (!StringUtil.isEmptyOrBlank(user.PhoneNumber))
                usersQuery = usersQuery.Where(u => u.PhoneNumber.Contains(user.PhoneNumber));

            if (usersQuery != null)
                return usersQuery.ToList<User>();

            return new List<User>();
        }

        public override bool Insert(User obj)
        {
            User user = obj;

            _context.Users.AddObject(user);
            _context.SaveChanges();

            return true;
        }

        public override bool Update(User obj)
        {
            User user = obj;

            User userRow = _context.Users.Single(
                u => u.Username == user.Username);
            userRow.Password = user.Password;
            userRow.RoleId = user.RoleId;
            _context.SaveChanges();

            return true;
        }

        public override bool DeleteById(string id)
        {
            bool deleted = false;
            string userId = id;
            var result = (from u in _context.Users
                          where u.Username.Equals(userId)
                          select u).FirstOrDefault();
            if (result != null)
            {
                _context.Users.DeleteObject((User)result);
                _context.SaveChanges();
                deleted = true;
            }

            return deleted;
        }
    }
}
